//
//  LXApplyAnnexTableCell.h
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//
#import "LXAnnexShowTableViewCell.h"

#import "LXApplyPicCollectionCell.h"


@interface LXAnnexShowTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;

@property (nonatomic) NSArray *dataSource;


@end


#define kItemHeight (KHeightScale(115))

#define kItemCount 3

@implementation LXAnnexShowTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
        
    }
    return self;
}

+(CGFloat)getCellHeightWithDataSource:(NSArray *)dataSource
{
    if (dataSource.count == 0) {
        return kComHeight;
    }
    CGFloat height = dataSource.count/kItemCount *kItemHeight;
    if (dataSource.count%kItemCount !=0 ){
        height = (dataSource.count/kItemCount +1) *kItemHeight ;
    }
    return height + 30;
}
- (void)reloadData:(NSArray *)dataSource{
    _dataSource = dataSource;
    
    CGFloat height = [LXAnnexShowTableViewCell getCellHeightWithDataSource:dataSource];
    self.collectionView.frame = CGRectMake(0, 30, kScreenWidth, height);
    
    self.subTitleLab.text = dataSource.count == 0?@"无":@"";
    
    [self.collectionView reloadData];
}

- (void)configUI{
    

    self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 200, 30)];
    self.titleLab.font = Fond16;
    self.titleLab.textColor = Color333;
    [self.contentView addSubview:self.titleLab];
    
    self.subTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.width - 15, 5, 35, 30)];
    self.subTitleLab.textAlignment = NSTextAlignmentRight;
    self.subTitleLab.font = Fond16;
    self.subTitleLab.textColor = Color333;
    [self.contentView addSubview:self.subTitleLab];
    
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(kScreenWidth/kItemCount,kItemHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing= 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:self.collectionView];
    
    self.collectionView.scrollEnabled = NO;
    
    [self.collectionView registerClass:[LXApplyPicCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([LXApplyPicCollectionCell class])];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LXApplyPicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXApplyPicCollectionCell class]) forIndexPath:indexPath];
    
    
    NSString *url = self.dataSource[indexPath.item];
    if (![url hasPrefix:@"http"]) {

        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:[HTTP_URL stringByAppendingString:url]] placeholderImage:Image(@"default_icon")];
        
    }
    else {
        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:Image(@"default_icon")];
    }
    cell.takeImgV.hidden = YES;
    cell.imgV.hidden = NO;
    cell.imgV.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    cell.deleteImg.hidden = YES;
    
    WEAKSELF
    //放大
    cell.extendImgBlock = ^(id  _Nonnull obj) {
        if (weakSelf.callBackExtendAnnex) {
            weakSelf.callBackExtendAnnex(indexPath.item,obj);
        }
    };
    
    return cell;
    
}



@end
